<?php

namespace App\Admin\Controllers\funds;

use App\Common\statement\CommonStatement;
use App\Models\fund\Recharge;
use Encore\Admin\Controllers\AdminController;
use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Show;

class RechargeController extends AdminController
{
    /**
     * Title for current resource.
     *
     * @var string
     */
    protected $title = '充值记录';

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        $grid = new Grid(new Recharge());
        $grid->model()->orderByDesc('id');
        //去除批量删除操作
        $grid->batchActions(function ($batch) {
            $batch->disableDelete();
        });

        $grid->disableCreateButton();

        // 行操作全部关闭
        $grid->disableActions();


        $grid->filter(function($filter){
            // 去掉默认的id过滤器
            $filter->disableIdFilter();

            $filter->column(1/3, function ($filter) {

                $filter->where(function ($query) {

                    $query->whereHas('user',function ($query){
                        $query->where('uuid', 'like', "%{$this->input}%");
                    })
                        ->orWhere('id', 'like', "%{$this->input}%");

                },__('Uuid'));

                $filter->where(function ($query) {

                    $query->where('out_trade_no', 'like', "%{$this->input}%")
                        ->orWhere('trade_no', 'like', "%{$this->input}%");

                },__('订单号'));


            });

            $filter->column(1/3, function ($filter) {


                $filter->where(function ($query) {

                    $query->whereHas('user',function ($query){
                        $query->where('phone', 'like', "%{$this->input}%")
                            ->orWhere('name', 'like', "%{$this->input}%");
                    });

                },'昵称手机');

            });


            $filter->column(1/3, function ($filter) {
                $filter->between('created_at',__('Created at'))->datetime();
            });

        });
        $grid->header(function ($query) {

            // 查询出已支付状态的订单总金额
            $data = $query->sum('pay_money');

            return "<div style='padding: 10px;'>总充值额 ： $data</div>";
        });

//        $grid->column('id', __('Id'));
        $grid->column('user_id', __('User id'));
        $grid->column('user.header_img', __('header_img'))
            ->lightbox(['width' => 50, 'height' => 50, 'class' => ['circle', 'thumbnail']]);

        $grid->column('user.name', __('Name'));
        $grid->column('user.phone', __('Phone'));
        $grid->column('out_trade_no', __('第三方订单号'));
        $grid->column('trade_no', __('Trade no'));

        $grid->column('pay_type', __('支付方式'))->display(function () {

            return CommonStatement::pay_type[$this->pay_type];

        });

        $grid->column('pay_money', __('付款金额'));


        $grid->column('title', __('Title'));
        $grid->column('created_at', __('Created at'));
        $grid->column('updated_at', __('Updated at'));

        return $grid;
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     * @return Show
     */
    protected function detail($id)
    {
        $show = new Show(Recharge::findOrFail($id));

        $show->field('id', __('Id'));
        $show->field('user_id', __('User id'));
        $show->field('out_trade_no', __('第三方订单号'));
        $show->field('trade_no', __('Trade no'));
        $show->field('pay_type', __('支付方式'));
        $show->field('pay_money', __('付款金额'));
        $show->field('type', __('Type'));
        $show->field('title', __('Title'));
        $show->field('created_at', __('Created at'));
        $show->field('updated_at', __('Updated at'));

        return $show;
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        $form = new Form(new Recharge);

        $form->number('user_id', __('User id'));
        $form->text('out_trade_no', __('第三方订单号'));
        $form->text('trade_no', __('Trade no'));
        $form->switch('pay_type', __('支付方式'));
        $form->decimal('pay_money', __('付款金额'));
        $form->switch('type', __('Type'))->default(1);
        $form->text('title', __('Title'));

        return $form;
    }
}
